<?php
// $Id: functions.php,v 1.2 2005/03/18 12:52:24 onokazu Exp $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //

function mainheader($mainlink=1) {
		echo "<br /><br /><p><a href=\"".XOOPSMYDOWNLOADURL."/index.php\"><img src=\"".mydownloadsGetIconURL("logo-en.gif")."\" border=\"0\" alt\"\" /></a></p><br /><br />";
}

function newdownloadgraphic($time, $status) {
	$count = 7;
	$new = '';
	$startdate = (time()-(86400 * $count));
		if ($startdate < $time) {
		if($status==1){
			//$new = "&nbsp;<img src=\"".XOOPS_URL."/modules/mydownloads/images/newred.gif\" alt=\""._MD_NEWTHISWEEK."\" />";
			$new = "&nbsp;<img src=\"".mydownloadsGetIconURL('newred.gif')."\" alt=\""._MD_NEWTHISWEEK."\" />";
		}elseif($status==2){
			//$new = "&nbsp;<img src=\"".XOOPS_URL."/modules/mydownloads/images/update.gif\" alt=\""._MD_UPTHISWEEK."\" />";
			$new = "&nbsp;<img src=\"".mydownloadsGetIconURL('update.gif')."\" alt=\""._MD_UPTHISWEEK."\" />";
		}
	}
	return $new;
}

function popgraphic($hits) {
	global $xoopsModuleConfig;
	if ($hits >= $xoopsModuleConfig['popular']) {
		//return "&nbsp;<img src =\"".XOOPS_URL."/modules/mydownloads/images/pop.gif\" alt=\""._MD_POPULAR."\" />";
		return "&nbsp;<img src=\"".mydownloadsGetIconURL('pop.gif')."\" alt=\""._MD_POPULAR."\" />";
	}
	return '';
}
//Reusable Link Sorting Functions
function convertorderbyin($orderby) {
	switch (trim($orderby)) {
	case "titleA":
		$orderby = "title ASC";
		break;
	case "dateA":
		$orderby = "date ASC";
		break;
	case "hitsA":
		$orderby = "hits ASC";
		break;
	case "ratingA":
		$orderby = "rating ASC";
		break;
	case "titleD":
		$orderby = "title DESC";
		break;
	case "hitsD":
		$orderby = "hits DESC";
		break;
	case "ratingD":
		$orderby = "rating DESC";
		break;
	case"dateD":
	default:
		$orderby = "date DESC";
		break;
	}
	return $orderby;
}
function convertorderbytrans($orderby) {
	if ($orderby == "hits ASC")   $orderbyTrans = _MD_POPULARITYLTOM;
	if ($orderby == "hits DESC")    $orderbyTrans = _MD_POPULARITYMTOL;
	if ($orderby == "title ASC")    $orderbyTrans = _MD_TITLEATOZ;
	if ($orderby == "title DESC")   $orderbyTrans = _MD_TITLEZTOA;
	if ($orderby == "date ASC") $orderbyTrans = _MD_DATEOLD;
	if ($orderby == "date DESC")   $orderbyTrans = _MD_DATENEW;
	if ($orderby == "rating ASC")  $orderbyTrans = _MD_RATINGLTOH;
	if ($orderby == "rating DESC") $orderbyTrans = _MD_RATINGHTOL;
	return $orderbyTrans;
}
function convertorderbyout($orderby) {
	if ($orderby == "title ASC")            $orderby = "titleA";
	if ($orderby == "date ASC")            $orderby = "dateA";
	if ($orderby == "hits ASC")          $orderby = "hitsA";
	if ($orderby == "rating ASC")        $orderby = "ratingA";
	if ($orderby == "title DESC")              $orderby = "titleD";
	if ($orderby == "date DESC")            $orderby = "dateD";
	if ($orderby == "hits DESC")          $orderby = "hitsD";
	if ($orderby == "rating DESC")        $orderby = "ratingD";
	return $orderby;
}

function PrettySize($size) {
	$mb = 1024*1024;
	if ( $size > $mb ) {
		$mysize = sprintf ("%01.2f",$size/$mb) . " MB";
	}
	elseif ( $size >= 1024 ) {
		$mysize = sprintf ("%01.2f",$size/1024) . " KB";
	}
	else {
	    $mysize = sprintf(_MD_NUMBYTES,$size);
	}
	return $mysize;
}

//updates rating data in itemtable for a given item
function updaterating($sel_id){
	global $xoopsDB;
	global $mydirname;

	$query = "select rating FROM ".$xoopsDB->prefix($mydirname."_votedata")." WHERE lid = ".$sel_id."";
	$voteresult = $xoopsDB->query($query);
		$votesDB = $xoopsDB->getRowsNum($voteresult);
	$totalrating = 0;
		while(list($rating)=$xoopsDB->fetchRow($voteresult)){
		$totalrating += $rating;
	}
	$finalrating = $totalrating/$votesDB;
	$finalrating = number_format($finalrating, 4);
	$sql = sprintf("UPDATE %s SET rating = %u, votes = %u WHERE lid = %u", $xoopsDB->prefix($mydirname."_downloads"), $finalrating, $votesDB, $sel_id);
	$xoopsDB->query($sql);
}

//returns the total number of items in items table that are accociated with a given table $table id
function getTotalItems($sel_id, $status=""){
	global $xoopsDB, $mytree;
	global $mydirname;

	$count = 0;
	$arr = array();
	$query = "select count(*) from ".$xoopsDB->prefix($mydirname."_downloads")." where cid=".$sel_id."";
	if($status!=""){
		$query .= " and status>=$status";
	}
	$result = $xoopsDB->query($query);
	list($thing) = $xoopsDB->fetchRow($result);
	$count = $thing;
	$arr = $mytree->getAllChildId($sel_id);
	$size = count($arr);
	for($i=0;$i<$size;$i++){
		$query2 = "select count(*) from ".$xoopsDB->prefix($mydirname."_downloads")." where cid=".$arr[$i]."";
		if($status!=""){
			$query2 .= " and status>=$status";
		}
		$result2 = $xoopsDB->query($query2);
		list($thing) = $xoopsDB->fetchRow($result2);
		$count += $thing;
	}
	return $count;
}

//wanikoo
function mydownloadsGetStyleURL($aFile){
	global $mydownloads_theme;
	$StyleURL = XOOPSMYDOWNLOADINCURL."/".$mydownloads_theme."/".$aFile;

	if ( file_exists(XOOPSMYDOWNLOADINCPATH."/".$mydownloads_theme."/".$aFile)){
		return $StyleURL;
	}else{
		return XOOPSMYDOWNLOADINCURL."/".$aFile;
	}
}
//
function mydownloadsGetIconURL($aFile){
	global $mydownloads_theme;
	$IconURL = XOOPSMYDOWNLOADIMGURL."/".$mydownloads_theme."/".$aFile;

	if ( file_exists(XOOPSMYDOWNLOADIMGPATH."/".$mydownloads_theme."/".$aFile)){
		return $IconURL;
	}else{
		return XOOPSMYDOWNLOADIMGURL."/".$aFile;
	}
}

//
function md_wfd_letters()
{
    global $xoopsModule;

    $letterchoice = "<div>" . _MD_BROWSETOTOPIC . "</div>";
    $letterchoice .= "[  ";
    $alphabet = array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
    $num = count($alphabet) - 1;
    $counter = 0;
    while (list(, $ltr) = each($alphabet))
    {
        $letterchoice .= "<a href='" .XOOPSMYDOWNLOADURL. "/viewcat.php?list=$ltr'>$ltr</a>";
        if ($counter == round($num / 2))
            $letterchoice .= " ]<br />[ ";
        elseif ($counter != $num)
            $letterchoice .= "&nbsp;|&nbsp;";
        $counter++;
    }
    $letterchoice .= " ]";
    return $letterchoice;
}

function md_wfd_isnewimage($published)
{
    global $xoopsDB;

    $oneday = (time() - (86400 * 1));
    $threedays = (time() - (86400 * 3));
    $week = (time() - (86400 * 7));

    if ($published > 0 && $published < $week)
    {
        $indicator['image'] = "images/icon/download4.gif";
        $indicator['alttext'] = _MD_NEWLAST;
    } elseif ($published >= $week && $published < $threedays)
    {
        $indicator['image'] = "images/icon/download3.gif";
        $indicator['alttext'] = _MD_NEWTHIS;
    } elseif ($published >= $threedays && $published < $oneday)
    {
        $indicator['image'] = "images/icon/download2.gif";
        $indicator['alttext'] = _MD_THREE;
    } elseif ($published >= $oneday)
    {
        $indicator['image'] = "images/icon/download1.gif";
        $indicator['alttext'] = _MD_TODAY;
    }
    else
    {
        $indicator['image'] = "images/icon/download.gif";
        $indicator['alttext'] = _MD_NO_FILES;
    }
    return $indicator;
}

function md_wfd_toolbar()
{
    global $xoopsModuleConfig, $xoopsUser;
       global $mydirname;

    $submissions = 0;
    if (is_object($xoopsUser))
    {
    $submissions = 1;
    }
    elseif (!is_object($xoopsUser) && $xoopsModuleConfig['anonpost'])
    {
    $submissions = 1;
    }
	
    $toolbar = "[ <a href='index.php'>" ._MD_MAIN. "</a> | ";
    if ($submissions == 1)
    {
        $toolbar .= "<a href='submit.php'>".constant("_MI_".strtoupper($mydirname)."_SMNAME1")."</a> | ";
    }
    $toolbar .= "<a href='topten.php?hit=1'>".constant("_MI_".strtoupper($mydirname)."_SMNAME2")."</a> | <a href='topten.php?rate=1'>".constant("_MI_".strtoupper($mydirname)."_SMNAME3")."</a> ]";
    return $toolbar;
}

?>